using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
namespace Test.Support {
	class BulkFileGener {
		static void MainC() {
			DAL dal=new DAL("pon-wpsq16-db","Directory");


			DataTable locs=dal.Query("select location,count(*)cnt from BulkPutaway where status='C' and sentstatus='U' and datediff(day,inserted-14./24,getdate())>0 group by location");
			List<string> locsLst=new List<string>();
			foreach(DataRow locRow in locs.Rows)
				locsLst.Add(locRow["location"].ToString());

			int rows=dal.NonQry("update BulkPutaway set sentstatus='P' where location in('{0}')",string.Join("','",locsLst.ToArray()));
			Console.WriteLine(rows);
			DataTable tab=dal.Query(@"select location,sku,sum(qty)qty from BulkPutaway(nolock) where sentstatus='P' group by location,sku order by max(id)");
			StringBuilder sb=new StringBuilder();
			foreach(DataRow row in tab.Rows)
			{
				string loc=row["location"] as string;
				string sku=row["sku"] as string;
				int qty=(int)row["qty"];
				sb.AppendLine(loc.Substring(0,6)+sku.Substring(0,8)+qty.ToString("00000"));
				DataTable det=dal.Query("select sku,qty from BulkPutaway(nolock) where status='C' and location='{0}'",loc);
				foreach(DataRow detrow in det.Rows)
				{
					int dqty=(int)detrow["qty"];
					string dsku=detrow["sku"] as string;
					sb.AppendLine("EW001A"+dsku.Substring(0,8)+dqty.ToString("00000"));
				}
			}
			if(tab.Rows.Count>0)
			{
				File.WriteAllText(@"C:\BulkPutFiles\BulkPutaway"+DateTime.Now.ToString("yyyyMMdd_HHmm")+".txt",sb.ToString());
				int rows2=dal.NonQry("update BulkPutaway set sentstatus='S' where sentstatus='P'");
				Console.WriteLine(rows2);
			}
		}
		static void MainX() {
			DAL dal=new DAL("pon-wpsq16-db","Directory");
			foreach(string str in File.ReadAllLines(@"C:\BulkPutFiles\xxxxxxx.txt"))
				if(!str.StartsWith("EW001"))
				{
					string loc=str.Substring(0,6);
					string sku=str.Substring(6,8);
					DataTable tab=dal.Query("select * from BulkPutaway where location like'{0}%' and sku like '{1}%'",loc,sku);
					dal.NonQry("update BulkPutaway set sentstatus='U' where location like'{0}%' and sku like '{1}%'",loc,sku);
				}
		}

		static void MainV() {
			string first=File.ReadAllText(@"C:\BulkPutFiles\BulkPutaway20140627_1544.txt");
			foreach(string str in File.ReadAllLines(@"C:\BulkPutFiles\BulkPutaway20140630_1644.txt"))
				if(!str.StartsWith("EW001"))
				{
					string loc=str.Substring(0,6);
					if(first.Contains(loc))
						Console.WriteLine(loc);

				}




		}
	}
}
